package com.atguigu.tingshu.order.strategy;


import cn.hutool.db.sql.Order;
import com.alibaba.nacos.plugin.auth.constant.Constants;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderDerate;
import com.atguigu.tingshu.model.order.OrderDetail;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.mapper.OrderDerateMapper;
import com.atguigu.tingshu.order.mapper.OrderDetailMapper;
import com.atguigu.tingshu.order.mapper.OrderInfoMapper;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

//保存订单数据的策越接口
public interface SaveOrderStrategy {

    // 保存订单数据的抽象方法
    public abstract OrderInfoVo saveOrderInfo(OrderInfoVo orderInfoVo);


    public abstract String getPayWay();

    //成功之后,保存订单的基本数据,和明细数据和减免数据

    public default void  saveOrderInfoAndDetailAndDerateInfo(OrderInfoVo orderInfoVo , OrderInfoMapper orderInfoMapper ,
                                                             OrderDetailMapper orderDetailMapper ,
                                                             OrderDerateMapper orderDerateMapper){

        Long userId  = AuthContextHolder.getUserId();

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setUserId(userId);
        List<OrderDetailVo> detailVoList = orderInfoVo.getOrderDetailVoList();
        OrderDetailVo orderDetailVo = detailVoList.get(0);
        orderInfo.setOrderTitle(orderDetailVo.getItemName());
        orderInfo.setOrderNo(orderInfoVo.getTradeNo());
        orderInfo.setOrderStatus(SystemConstant.ORDER_STATUS_UNPAID);
        orderInfo.setOriginalAmount(orderInfoVo.getOriginalAmount());
        orderInfo.setDerateAmount(orderInfoVo.getDerateAmount());
        orderInfo.setOrderAmount(orderInfoVo.getOrderAmount());
        orderInfo.setItemType(orderInfoVo.getItemType());
        orderInfo.setPayWay(orderInfoVo.getPayWay());
        //保存订单数据
        orderInfoMapper.insert(orderInfo);
        //保存订单明细数据
        detailVoList.forEach(odv->{
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderInfo.getId());
            BeanUtils.copyProperties(odv,orderDetail);
            orderDetailMapper.insert(orderDetail);
        });

       //保存订单减免数据 判断减免的数据 存不存在
        List<OrderDerateVo> orderDerateVoList = orderInfoVo.getOrderDerateVoList();
        if (!CollectionUtils.isEmpty(orderDerateVoList)) {
            orderDerateVoList.forEach(orderDerateVo -> {
                OrderDerate orderDerate = new OrderDerate();
                orderDerate.setOrderId(orderInfo.getId());
                BeanUtils.copyProperties(orderDerateVo, orderDerate);
                orderDerateMapper.insert(orderDerate);
            });
        }

        // 执行后续操作
        this.saveOrderAfterProcess(orderInfoVo);

    }

    //执行后续操作的接口任务,让实现类去重写
    public abstract void saveOrderAfterProcess(OrderInfoVo orderInfoVo);
}
